﻿using DotNetty.Buffers;
using DotNettyExtensions;
using DotNettyExtensions.entity;
using EasyIot.Common;
using EasyIot.Repository;
using EasyIot.WebApi.Utility;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Net;
using System.Text;

namespace EasyIot.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MainController : ControllerBase
    {
        /// <summary>
        /// 通过sql语句查询
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<string> Download([FromBody] JsonRequest1 request)
        {
            var result = new Response<string>();

            try
            {
                string content = request.content; Console.WriteLine("Download,content=" + content);

                string strInput = DESEncrypt.Decrypt(content);
                var dt = BaseRepositoryExt.Query(strInput);
                string info = JsonConvert.SerializeObject(dt, new DataTableConverter());
                string strOutput = DESEncrypt.Encrypt(info);
                result.Result = strOutput;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 通过sql语句更新
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<string> Upload([FromBody] JsonRequest1 request)
        { 
            var result = new Response<string>();

            try
            {
                string content = request.content; Console.WriteLine("Upload,content=" + content);

                string strInput = DESEncrypt.Decrypt(content);
                string info = BaseRepositoryExt.Excute(strInput).ToString();
                string strOutput = DESEncrypt.Encrypt(info);
                result.Result = strOutput;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }
           
            return result;
        } 

        /// <summary>
        /// 业务处理接口
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<string> Process([FromBody] JsonRequest1 request)
        {
            
            var result = new Response<string>();

            try
            {
                string content = request.content; Console.WriteLine("Process,content=" + content);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 下发控制接口
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<string> Control([FromBody] JsonRequest2 request)
        {  
            var result = new Response<string>();

            try
            {
                string uid = request.uid; string content = request.content; string input1 = DESEncrypt.Decrypt(uid); string input2 = DESEncrypt.Decrypt(content);

                input1 = uid; input2 = content;// test

                Console.WriteLine("Control,uid=" + input1 + ",content=" + input2);

                string errmsg = string.Empty;
                UidEntity entity = ServerMgr.CheckDevice(input1, ref errmsg);
                if (!string.IsNullOrEmpty(errmsg))
                {
                    result.Code = 500;
                    result.Message = errmsg;
                }
                else
                { 
                    ProtocolBase.SendProtocol(entity.Channel, entity.Remote, input1, 3, input2);
                }
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }

            return result;
        } 
    }
}
